package com.sbtr.business.classify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sbtr.business.classify.entity.AlgoParamVersion;
import com.sbtr.business.classify.dto.AlgoParamVersionDto;

import com.sbtr.business.classify.entity.AlgoTemplateParam;
import com.sbtr.business.classify.service.AlgoParamVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.classify.mapper.AlgoParamVersionMapper;
import com.sbtr.business.classify.service.AlgoTemplateParamService;
import com.sbtr.business.feature.entity.FeDatafile;
import com.sbtr.business.feature.entity.FeOperator;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.common.PageDto;
import com.sbtr.util.PageDtoUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 轻量化算法参数版本(高质量芯片项目) Service接口实现类
 *
 * @author maojianping
 * @since  2023-04-17
 */
@Transactional
@Service
public class AlgoParamVersionServiceImpl extends ServiceImpl<AlgoParamVersionMapper,AlgoParamVersion> implements AlgoParamVersionService {

/*    @Autowired
    AlgoParamVersionMapper algoParamVersionMapper;*/

    @Autowired
    AlgoTemplateParamService algoTemplateParamService;

    @Override
    public PageDto<AlgoParamVersion> pageList(AlgoParamVersionDto algoParamVersionDto) {

        IPage<AlgoParamVersion> page = new Page<>();
        page.setSize(algoParamVersionDto.getPageSize());
        page.setCurrent(algoParamVersionDto.getPage());
        page = baseMapper.selectPages(page, BeanUtil.beanToMap(algoParamVersionDto));
        PageDto<AlgoParamVersion> r = PageDtoUtils.getPageDtoByIPage(page, AlgoParamVersion.class);

        return r;
    }

    @Override
    public AlgoParamVersion copyNewVersion(String username, AlgoParamVersion algoVersion, List<AlgoTemplateParam> paramList) {

        // 1.基于默认版本，创建新算法参数版本
        algoVersion.setId(null);
        algoVersion.setEditor(username); // 更新编辑者
        algoVersion.setUpdateTime(new Date());
        baseMapper.insert(algoVersion);

        // 2.基于默认版本算法参数，创建新版本参数
        for(AlgoTemplateParam param:paramList){
            param.setId(null); // 置空
            param.setParamVersionId(algoVersion.getId()); // 使用新版本的ID外键
        }
        algoTemplateParamService.saveBatch(paramList); // 更新保存
        return algoVersion;
    }

    @Override
    public List<AlgoParamVersion> queryList(Long templateId) {

        QueryWrapper<AlgoParamVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("template_id", templateId);
        List<AlgoParamVersion> list = baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public boolean removeById(Serializable id){
        // 删除算法参数版本
        int count = baseMapper.deleteById(id);
        //根据参数历史版本对象，删除对应参数集合
        boolean flag = algoTemplateParamService.delByVersionId(id);
        return count>0 && flag;
    }

}

